package com.zx.zxpicture.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.zxpicture.application.service.UserApplicationService;
import com.zx.zxpicture.domain.user.entity.User;
import com.zx.zxpicture.domain.user.objectvalue.UserRoleEnum;
import com.zx.zxpicture.domain.user.service.UserDomainService;
import com.zx.zxpicture.interfaces.dto.user.UserQueryRequest;
import com.zx.zxpicture.interfaces.dto.user.UserRegisterRequest;
import com.zx.zxpicture.interfaces.vo.LoginUserVO;
import com.zx.zxpicture.interfaces.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 用户服务实现
 */
@Service
@Slf4j
public class UserApplicationServiceServiceImpl implements UserApplicationService {

    @Resource
    private UserDomainService userDomainService;
    /**
     * 盐值，混淆密码
     */
    public static final String SALT = "yupi";

    /**
     * 用户注册
     * @param userRegisterRequest 用户注册请求
     * @return
     */
    @Override
    public long userRegister(UserRegisterRequest userRegisterRequest) {
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        // 1. 校验
        User.validUserRegister(userAccount, userPassword, checkPassword);
        return userDomainService.userRegister(userAccount, userPassword, checkPassword);
    }

    /**
     * 用户登录
     * @param userAccount  用户账户
     * @param userPassword 用户密码
     * @param request
     * @return
     */
    @Override
    public LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request) {
        // 1. 校验
        User.validUserLogin(userAccount, userPassword);
        return userDomainService.userLogin(userAccount, userPassword, request);
    }

    @Override
    public long addUser(User user) {
        return userDomainService.addUser(user);
    }


    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @Override
    public User getLoginUser(HttpServletRequest request) {
       return userDomainService.getLoginUser(request);
    }


    /**
     * 是否为管理员
     *
     * @param request
     * @return
     */
    @Override
    public boolean isAdmin(HttpServletRequest request) {
       return userDomainService.isAdmin(request);
    }

    @Override
    public boolean isAdmin(User user) {
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }

    /**
     * 用户注销
     *
     * @param request
     */
    @Override
    public boolean userLogout(HttpServletRequest request) {
        return userDomainService.userLogout(request);
    }

    @Override
    public LoginUserVO getLoginUserVO(User user) {
       return userDomainService.getLoginUserVO(user);
    }

    @Override
    public UserVO getUserVO(User user) {
        return userDomainService.getUserVO(user);
    }

    @Override
    public List<UserVO> getUserVO(List<User> userList) {
      return userDomainService.getUserVO(userList);
    }

    @Override
    public QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
       return userDomainService.getQueryWrapper(userQueryRequest);
    }

    @Override
    public boolean removeById(Long id) {
        return userDomainService.removeById(id);
    }

    @Override
    public boolean updateById(User user) {
        return userDomainService.updateById(user);
    }

    @Override
    public User getById(long id) {
        return userDomainService.getById(id);
    }

    @Override
    public Page<User> page(Page<User> page, QueryWrapper<User> queryWrapper) {
        return userDomainService.page(page,queryWrapper);
    }

    @Override
    public Collection<User> listByIds(Set<Long> userIds) {
        return userDomainService.listByIds(userIds);
    }
}
